﻿using System;
using System.ComponentModel.Design;
using SedionFormDesigner.Infrastructure;
using System.Drawing.Design;
using System.Windows.Forms;

namespace SedionFormDesigner.Service
{
    /// <summary>
    ///  是设计器与工具箱交流
    /// </summary>
    public class SedionToolboxService : System.Drawing.Design.IToolboxService
    {
        private IDesignerHost host;
        private ToolboxPane toolbox;

        public SedionToolboxService(IDesignerHost host)
        {
            this.host = host;

            toolbox = host.GetService(typeof(ToolboxPane)) as ToolboxPane;
        }

        #region IToolboxService 成员

        /// <summary>
        ///  添加一个构造器,创建特殊的toolboxItem
        ///  不支持
        /// </summary>
        public void AddCreator(System.Drawing.Design.ToolboxItemCreatorCallback creator, string format, System.ComponentModel.Design.IDesignerHost host)
        {
            // 不支持
        }

        /// <summary>
        ///  添加一个构造器,创建特殊的toolboxItem
        ///  不支持
        /// </summary>
        public void AddCreator(System.Drawing.Design.ToolboxItemCreatorCallback creator, string format)
        {
            // 不支持
        }

        /// <summary>
        ///  添加toolboxItem到toolbox控件中,绑定到host里
        ///  不支持
        /// </summary>
        public void AddLinkedToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem, string category, System.ComponentModel.Design.IDesignerHost host)
        {
            // 不支持...
        }

        /// <summary>
        ///  添加toolboxItem到toolbox控件中,绑定到host里
        ///  不支持
        /// </summary>
        public void AddLinkedToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem, System.ComponentModel.Design.IDesignerHost host)
        {
            // 不支持...
        }

        /// <summary>
        ///  添加toolboxItem
        ///  不支持
        /// </summary>
        public void AddToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem, string category)
        {
            // 不支持
        }

        /// <summary>
        ///  添加toolboxItem
        ///  不支持
        /// </summary>
        public void AddToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem)
        {
            // 不支持
        }

        /// <summary>
        ///  获得分类列表
        /// </summary>
        public System.Drawing.Design.CategoryNameCollection CategoryNames
        {
            get { return toolbox.CategoryNames; }
        }

        /// <summary>
        ///  在将ToolboxItem拽到设计面板是进行反序列化
        ///  原设计是只有一个host的所以无视host...
        ///  跟我们的想法可能由出入...
        /// </summary>
        public System.Drawing.Design.ToolboxItem DeserializeToolboxItem(object serializedObject, System.ComponentModel.Design.IDesignerHost host)
        {
            return (ToolboxItem)((DataObject)serializedObject).GetData(typeof(ToolboxItem));
        }

        public System.Drawing.Design.ToolboxItem DeserializeToolboxItem(object serializedObject)
        {
            return this.DeserializeToolboxItem(serializedObject, this.host);
        }

        /// <summary>
        ///  获得选中的工具项
        /// </summary>
        public System.Drawing.Design.ToolboxItem GetSelectedToolboxItem(System.ComponentModel.Design.IDesignerHost host)
        {
            if (toolbox.SelectedTool.DisplayName != ToolboxPane.Pointer)
                return toolbox.SelectedTool;
            return null;
        }

        /// <summary>
        ///  获得选中的工具项
        /// </summary>
        public System.Drawing.Design.ToolboxItem GetSelectedToolboxItem()
        {
            return this.GetSelectedToolboxItem(this.host);
        }

        /// <summary>
        ///  获得当前分类的所有工具项
        /// </summary>
        public System.Drawing.Design.ToolboxItemCollection GetToolboxItems(string category, System.ComponentModel.Design.IDesignerHost host)
        {
            return toolbox.GetTools();
        }

        /// <summary>
        ///  获得当前分类的所有工具项
        /// </summary>
        public System.Drawing.Design.ToolboxItemCollection GetToolboxItems(string category)
        {
            return toolbox.GetTools();
        }

        /// <summary>
        ///  获得所有工具项
        /// </summary>
        public System.Drawing.Design.ToolboxItemCollection GetToolboxItems(System.ComponentModel.Design.IDesignerHost host)
        {
            return toolbox.GetTools();
        }

        /// <summary>
        ///  获得所有工具项
        /// </summary>
        public System.Drawing.Design.ToolboxItemCollection GetToolboxItems()
        {
            return toolbox.GetTools();
        }

        /// <summary>
        ///  原设计是使用标准控件,所以support
        ///  我们还是得运行起来看会不会出错...
        /// </summary>
        public bool IsSupported(object serializedObject, System.Collections.ICollection filterAttributes)
        {
            return true;
        }

        /// <summary>
        ///  原设计是使用标准控件,所以support
        ///  我们还是得运行起来看会不会出错...
        /// </summary>
        public bool IsSupported(object serializedObject, System.ComponentModel.Design.IDesignerHost host)
        {
            return true;
        }

        /// <summary>
        ///  检查序列化的对象是否为ToolboxItem
        /// </summary>
        public bool IsToolboxItem(object serializedObject, System.ComponentModel.Design.IDesignerHost host)
        {
            if (this.DeserializeToolboxItem(serializedObject, host) != null)
                return true;
            return false;
        }

        /// <summary>
        ///  检查序列化的对象是否为ToolboxItem
        /// </summary>
        public bool IsToolboxItem(object serializedObject)
        {
            return IsToolboxItem(serializedObject, this.host);
        }

        /// <summary>
        ///  刷新工具箱
        /// </summary>
        public void Refresh()
        {
            toolbox.Refresh();
        }

        /// <summary>
        ///  移除构造器
        ///  不支持
        /// </summary>
        public void RemoveCreator(string format, System.ComponentModel.Design.IDesignerHost host)
        {
            // 不支持
        }

        /// <summary>
        ///  移除构造器
        ///  不支持
        /// </summary>
        public void RemoveCreator(string format)
        {
            // 不支持
        }

        /// <summary>
        ///  移除工具项
        ///  不支持
        /// </summary>
        public void RemoveToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem, string category)
        {
            // 不支持
        }

        /// <summary>
        ///  移除工具项
        ///  不支持
        /// </summary>
        public void RemoveToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem)
        {
            // 不支持
        }

        public string SelectedCategory
        {
            get
            {
                return toolbox.SelectedCategory;
            }
            set
            {
                // 不支持
            }
        }

        /// <summary>
        ///  当ToolPicked方法被调用是触发,设置选中项为pointer
        /// </summary>
        public void SelectedToolboxItemUsed()
        {
            toolbox.SelectPointer();
        }

        /// <summary>
        ///  通过打包item序列化toolbox
        /// </summary>
        public object SerializeToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem)
        {
            return new DataObject(toolboxItem);
        }

        /// <summary>
        ///  如果要改变鼠标样式返回true
        /// </summary>
        public bool SetCursor()
        {
            if (toolbox.SelectedTool.DisplayName != ToolboxPane.Pointer)
            {
                Cursor.Current = Cursors.Cross;
                return true;
            }

            return false;
        }

        /// <summary>
        ///  设置选中的工具项
        /// </summary>
        /// <param name="toolboxItem"></param>
        public void SetSelectedToolboxItem(System.Drawing.Design.ToolboxItem toolboxItem)
        {
            // 不支持
        }

        #endregion
    }
}
